const express = require('express')
const router = express.Router()

function router_middleware(req, res, next) {
  console.log('router_middleware')
  next()
}
// 路由级别使用中间件
router.use(router_middleware)

// 验证登录参数
function valid_login_params(req, res, next) {
  let { name, password } = req.query

  if (!name || !password) {
    res.json({
      message: '参数校验失败'
    })
  } else {
    req.formdata = {
      name, password
    }
    next()
  }
}

router.get('/demo', [/**一些列中间件 */ valid_login_params], (req, res) => {
  let formdata = req.formdata
  res.json({
    formdata,
    message: 'router from demo'
  })
})

module.exports = router